use rustyline::{error::ReadlineError, history::FileHistory};
use std::{
    error::Error,
    process::{self},
};

type Result<T> = std::result::Result<T, Box<dyn Error>>;

fn get_last(rl: &mut FileHistory) -> Result<String> {
    Ok(String::new())
}
pub fn process() -> Result<()> {
    let mut rl = rustyline::DefaultEditor::new()?;

    rl.load_history("history.txt").unwrap_or(());
    loop {
        let readline = rl.readline(">> ");
        match readline {
            Ok(line) => {
                if rl.add_history_entry(line.as_str()).is_err() {
                    eprintln!("Add history failed");
                }
                println!("Line: {}", line);
                if line.ends_with('\\') {
                    println!("##############{}", line.len());
                }
            }
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                continue;
            }
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break;
            }
            Err(err) => {
                println!("Error: {:?}", err);
                process::exit(-1);
            }
        }
    }
    Ok(())
}

pub fn welcome() {
    println!("Welcome to HLisp.\nPress CTRL+D or exit() to quit.");
}
